#include "view.h"

View::View(Ui::BimosControl *uiBimosWindow):ui(uiBimosWindow) {
    setupActivityBox();
}



void View::setupActivityBox() {
    ui->horizontalLayout->setSpacing(0);
    ui->horizontalLayout->setMargin(0);
    ui->clBtn_calibrate->setEnabled(false);
    *activityLabels = new QLabel[15];
    *valueLabels = new QLabel[15];
    *placeholderLabels = new QLabel[8];
    labelCounter = 0;
    valueLabelCounter = 0;
    placeholderCounter = 0;

    lastConnectorActive = 0;

    for (int i = 0; i < 15; i++) {
        activityLabels[i] = new QLabel();
        valueLabels[i] = new QLabel(QString::number(i+1));
        valueLabels[i]->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    }
    for (int i = 0; i < 8; i++) {
        placeholderLabels[i] = new QLabel("|");
    }

    // init input labels for 0 - 11 with placeholders
    setupImageRow(0);
    setupValueRow(1);
    setupImageRow(2);
    setupValueRow(3);
    setupRefAndTemp();


    setupImagesForActivityBox();


    setupDrawingArea();

    setupDebugging();



}


 void View::setupDebugging() {
     *displayBottle = new QCheckBox[COUNTOFCONNECTORS];
     *displayValues = new QLabel[COUNTOFCONNECTORS];
     *displayPressure = new QLabel[COUNTOFCONNECTORS];

     for (int i = 0; i < COUNTOFCONNECTORS; i++) {
         displayBottle[i] = new QCheckBox(QString::number(i));
         displayValues[i] = new QLabel();
         displayPressure[i] = new QLabel();
         displayBottle[i]->setChecked(true);
     }

     for (int column = 0; column < COUNTOFCONNECTORS; column++) {
         ui->gridLayout_3->addWidget(displayBottle[column],0,column);
         ui->gridLayout_3->addWidget(displayValues[column],1,column);
         ui->gridLayout_3->addWidget(displayPressure[column],2,column);

     }
 }

void View::setupImagesForActivityBox() {
    loadImages();
    for (int i = 0 ; i < 15 ; i++) {
        activityLabels[i]->setPixmap(*inputInactive);
        activityLabels[i]->setScaledContents(true);
    }
}
void View::loadImages() {
    inputActive = new QPixmap(":/img/ps2/img/ps-active.png");
    inputInactive = new QPixmap(":/img/ps2/img/ps-inactive.png");

}

void View::setupImageRow(int gridRow) {
    for (int column = 0; column < 7; column++) {
        if (column == 3)
            ui->gridLayout->addWidget(placeholderLabels[placeholderCounter++],
                                      gridRow,column);
        else
            ui->gridLayout->addWidget(activityLabels[labelCounter++],gridRow,
                                      column);
    }
}

void View::setupValueRow(int gridRow) {
    for (int column = 0; column < 7; column++) {
        if (column == 3)
            ui->gridLayout->addWidget(placeholderLabels[placeholderCounter++],
                                      gridRow,column);
        else
            ui->gridLayout->addWidget(valueLabels[valueLabelCounter++],gridRow,
                                      column);
    }
}

void View::setupRefAndTemp() {
    // placeholders right of input boxes
    ui->gridLayout->addWidget(placeholderLabels[placeholderCounter++],0,7);
    ui->gridLayout->addWidget(placeholderLabels[placeholderCounter++],1,7);
    // image labels for reference bottles 1 and 2
    ui->gridLayout->addWidget(activityLabels[labelCounter++],0,8);
    ui->gridLayout->addWidget(activityLabels[labelCounter++],0,9);
    // image label for temperature input
    ui->gridLayout->addWidget(activityLabels[labelCounter++],2,9);

    ui->gridLayout->addWidget(valueLabels[valueLabelCounter++],1,8);
    ui->gridLayout->addWidget(valueLabels[valueLabelCounter++],1,9);

    ui->gridLayout->addWidget(valueLabels[valueLabelCounter++],3,9);
}


void View::setupDrawingArea() {
    //gsVolt = new QGraphicsScene(0,0,ui->graphicsView->width()-10,ui->graphicsView->height()-10);
    gsVolt = new QGraphicsScene();
    gsVolt->setItemIndexMethod(QGraphicsScene::NoIndex);
    ui->graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
    ui->graphicsView->setScene(gsVolt);
    oldX = 0;
    newX = 1;
    //newX = ui->graphicsView->width();

    for (int i = 0; i < COUNTOFCONNECTORS; i++)
        oldYofConnector[i] = 0;
}

void View::setTextValues(int conNumber, float value, float mult, float offset) {
    displayValues[conNumber]->setText("Volt:\n" + QString::number(value) + "\n");
    displayPressure[conNumber]->setText("Druck:\n" + QString::number(mult*value+offset));
}


void View::drawVoltValue(int conNumber,float value) {
/*
    int newY = ui->graphicsView->height() - value*140;
    if (newX%ui->graphicsView->width() >= ui->graphicsView->width()-1) {
        //oldX = 0;
        //gsVolt->clear();
        gsVolt->addLine(0,gsVolt->height() ,ui->graphicsView->width(),gsVolt->height(),QPen(QBrush(Qt::black),1));
        gsVolt->addLine(0,gsVolt->height() - 50,ui->graphicsView->width(),gsVolt->height() - 50,QPen(QBrush(Qt::black),1));
        gsVolt->addLine(0,gsVolt->height() - 100,ui->graphicsView->width(),gsVolt->height() - 100,QPen(QBrush(Qt::black),1));
        gsVolt->addLine(0,gsVolt->height() - 150,ui->graphicsView->width(),gsVolt->height() - 150,QPen(QBrush(Qt::black),1));
        //ui->graphicsView->setTransform(QTransform::fromScale(0.5,1));
    }

    gsVolt->addLine(oldX,oldYofConnector[conNumber],newX,newY,QPen(QBrush(Qt::red),1));

    oldYofConnector[conNumber] = newY;

    if (conNumber == lastConnectorActive)
        oldX = newX++;
*/
}

void View::setLastConnectorActive(int con) {
    lastConnectorActive=con;
}
